package top.datacluster.basic.algorithm.leetcode;

public class Main1 {

    public Node test(Node n1, Node n2){
        Node header = new Node(0);
        Node result = header;
        int flag = 0;
        while (n1 != null || n2 != null){
            int temp;

            if (n1 != null && n2 != null){
                temp = n1.val + n2.val + flag;
            }else if (n1 != null){
                temp = n1.val + flag;
            }else{
                temp = n2.val + flag;
            }

            if (temp >= 10){
                flag = 1;
                temp = temp % 10;
            }else{
                flag = 0;
            }
            header.next = new Node(temp);
            header = header.next;
            n1 = n1.next;
            n2 = n2.next;
        }

        return result.next;
    }

}

class Node{
    int val;
    Node next;

    public Node(int val) {
        this.val = val;
    }
}